<?php
namespace HC\Swoole;

use Psr\Log\LoggerInterface;

class Logger implements LoggerInterface
{
    public function log($level, $message, array $context = [])
    {
        $this->echo($level, $message, $context);
    }

    public function debug($message, array $context = [])
    {
        $this->echo('debug', $message, $context);
    }

    public function error($message, array $context = [])
    {
        $this->echo('error', $message, $context);
    }

    public function notice($message, array $context = [])
    {
        $this->echo('notice', $message, $context);
    }

    public function warning($message, array $context = [])
    {
        $this->echo('warning', $message, $context);
    }

    public function info($message, array $context = [])
    {
        $this->echo('info', $message, $context);
    }

    public function alert($message, array $context = [])
    {
        $this->echo('alert', $message, $context);
    }

    public function critical($message, array $context = [])
    {
        $this->echo('critical', $message, $context);
    }

    public function emergency($message, array $context = [])
    {
        $this->echo('emergency', $message, $context);
    }

    private function echo(string $level, string $message, array $context = [])
    {
        $message = sprintf("%s [%s]%s %s \n", date('Y-m-d H:i:s'), $level, $message, $context ? json_encode($context) : '');
        syslog(LOG_LOCAL6, $message);
        echo $message;
    }
}